Page({
  data: {
    videoList: [],
    total: 0,
    page: 1,
    size: 12,
    search_name: ''
  },
  onLoad() {
    this.getVideoList()
  },
  onShow() {

  },
  getInput(e) {
    // console.log('e',e.detail.value)
    this.setData({
      search_name: e.detail
    })
  },
  onSearch() {
    this.setData({
      page: 1
    })
    this.getVideoList()
  },
  // 获取数据
  getVideoList() {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: `http://1.15.142.179:4000/index?page=${this.data.page}&size=${this.data.size}&search_name=${this.data.search_name}`, //仅为示例，并非真实的接口地址
      success: (res) => {
        // console.log(res.data)
        if (res.data.ret === 0) {
          this.setData({
            videoList: this.data.page === 1 ? res.data.data.list : this.data.videoList.concat(res.data.data.list),
            total: res.data.data.total
          })
          let ids = this.data.videoList.map(c => {
            return c.vod_id
          })
          this.setData({
            ids
          })
          this.getPosterPic()
        }
      },
      fail: (err) => {
        console.log(err)
      },
      complete: () => {

      }
    })
  },
  // 获取封面图
  getPosterPic() {
    wx.showLoading({
      title: '加载中',
    })
    let ids = this.data.ids.slice(this.data.page * this.data.size - this.data.size, this.data.page * this.data.size)
    wx.request({
      url: `https://m3u8.apibdzy.com/api.php/provide/vod/?ac=detail&ids=${ids}`, //仅为示例，并非真实的接口地址
      success: (res) => {
        // console.log(res.data.list)
        for (let i = (this.data.page * this.data.size - this.data.size); i < this.data.videoList.length; i++) {
          for (let j = 0; j < res.data.list.length; j++) {
            if (Number(this.data.videoList[i].vod_id) === res.data.list[j].vod_id) {
              this.data.videoList[i].vod_pic = res.data.list[j].vod_pic
            }
          }
        }
        this.setData({
          videoList: this.data.videoList
        })
      },
      fail: (err) => {

      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  // 跳转详情
  toDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/video/video?id=${id}`,
    })
  },
  onReachBottom: function () {
    if (this.data.page < Math.ceil(this.data.total / this.data.size)) {
      this.setData({
        page: this.data.page + 1
      })
      this.getVideoList()
    }
  },
  onShareAppMessage() {

  }
})